package com.pactera.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.pactera.cache.Cache;
import com.pactera.consts.Const;
import com.pactera.pojo.CmrAjf;
import com.pactera.pojo.SmsPhone;
import com.pactera.utils.DateUtil;

/**
 * Created by mary on 2018/7/26.
 */
public class JobService {
	private static Logger logger = Logger.getLogger(JobService.class);
	
	public void doErrorTask(PostgresDBService postgresService,OracleDBService oracleService) {
		List<CmrAjf> smsList = postgresService.getErrorTask();
		
		if (smsList.size() == 0) return;

		// 跟上次一样的存入中间list
		List<CmrAjf> tmpList = new ArrayList<CmrAjf>();
		for (int i = 0; i < smsList.size(); i++) {
			for (int j = 0; j < Cache.EverSmslist.size(); j++) {
				if (smsList.get(i).getJobname().equals(Cache.EverSmslist.get(j).getJobname())
						&& smsList.get(i).getApplgroup().equals(Cache.EverSmslist.get(j).getApplgroup())
						&& smsList.get(i).getOdate().equals(Cache.EverSmslist.get(j).getOdate())) {
					tmpList.add(smsList.get(i));
				}
			}
		}
		//小于等于上次，并且没有新的的不发
		//准备发送的列表小于等于缓存的列表，并且准备发送的列表全在缓存中出现
		if(smsList.size()<=Cache.EverSmslist.size()&&tmpList.size()==smsList.size()){
			return;
		}
		// 准备短信内容
		String title = "类型1：截止" + DateUtil.DateToStr(new Date(),"MM月dd日HH") + "点，共有"
				+ smsList.size() + "条报错，分别为：";
		/*
		 *0 ELK_FDM.FDM_CDB_GL_D
		 *1 ELK_SDM.SDB_S01_BIFM24
		 *2 ELK_SDM.SDB_S01_GIFM21
		 *3 ELK_SDM.SDB_S05_DEP_QUETE_DETAIL
		 *4 ELK_SDM.SDB_S05_DL_DETAIL
		 *5 ELK_SDM.SDB_S05_DL_DETAIL2
		 */
		//jobNameSMS为往用户手机上发的内容: ELK_ODB(S13_MENEG,S13_MENEG)
		//jobNameDB为往数据库存日志的内容: ELK_ODM.S13_MENEG,ELK_ODM.S13_MENEG
		StringBuffer jobNameSMS = new StringBuffer();
		StringBuffer jobNameDB = new StringBuffer();
		String groupName = "~";
		int groupCount = 0;
		int prevGroupCount = 0;
		boolean sizeflag = false;
		//如果报错作业大于规定数目, 每个组只打印4条
		if(smsList.size()>Const.SMS_ELIPSIS_SIZE){
			sizeflag=true;
		}
		for (int i = 0; i < smsList.size(); i++) {
			//如果当前作业groupname和前一个不一样, 拼接groupname(
			if (!groupName.equals(smsList.get(i).getApplgroup())) {
				//重置group计数器
				prevGroupCount = groupCount;
				groupCount = 0;
				groupName = smsList.get(i).getApplgroup();
				//如果不是第一个作业在后面加),
				if(i!=0){
					jobNameSMS.deleteCharAt(jobNameSMS.length()-1);//ELK_FDM(FDM_CDB_GL_D等),
					if(sizeflag){
						if(prevGroupCount>4){
							jobNameSMS.append("等),");
						}else{
							jobNameSMS.append("),");
						}
					}else {
						jobNameSMS.append("),");
					}
				}
				jobNameSMS.append(smsList.get(i).getApplgroup());
				jobNameSMS.append("(");
			}
			jobNameDB.append(smsList.get(i).getApplgroup()+"."+smsList.get(i).getJobname());
			if(sizeflag){//ELK_FDM(FDM_CDB_GL_D等),ELK_SDM(SDB_S01_BIFM24,SDB_S01_GIFM21,SDB_S05_DEP_QUETE_DETAIL,SDB_S05_DL_DETAIL,
				groupCount=groupCount+1;
				if(groupCount>4){
					if (i == smsList.size() - 1) {
						jobNameSMS.deleteCharAt(jobNameSMS.length()-1);
						jobNameSMS.append("等)");
					}
					continue;
				}
			}
			jobNameSMS.append(smsList.get(i).getJobname());//ELK_FDM(FDM_CDB_GL_D
			//如果不是最后一个加逗号(,),否则关闭括号+)
			if (i != smsList.size() - 1) {
				jobNameSMS.append(",");
				jobNameDB.append(",");
			} else {
				jobNameSMS.append(")");
			}//ELK_FDM(FDM_CDB_GL_D,
		}
		// 开始获取短信号码，普通状态有且只有一条
		List<SmsPhone> smsPhoneList= oracleService.getSmsPhone(Const.SMS_TYPE_1);
		String phoneNum = smsPhoneList.get(0).getPhone_number();
		// 开始发短信
		logger.info("type1开始发短信,本次发送号码：" + phoneNum);
		//因为存放jobname数据字段类型为varchar2 , 所以最大字符数为4000
		String jobNameSmsStr = jobNameSMS.toString();
		if(jobNameSMS.length()>3999){
			jobNameSmsStr = jobNameSMS.substring(0,3999);
		}
		int result = SmsService.sendSms(Const.SMS_WSDL,
				Const.SMS_SP_CODE, Const.SMS_USER, Const.SMS_PWD,
				Const.SMS_SI, phoneNum, title + jobNameSmsStr);
		if (result==Const.SMS_SEND_SUCCESS) {
			// 如果成功发送短信，存入缓存防止下次重复发送
			Cache.EverSmslist = smsList;
		}
		// 插入日志表
		logger.info("type1开始保存短信日志");
		oracleService.saveSmsLog(phoneNum, jobNameDB.toString(), result,Const.SMS_TYPE_1);
	}

	
}
